﻿(function(window, undefined) {

var FOCUSEDMATCH = getCacheName("match", "football"),
    WebApi = window.WebApi;


// 抽象比赛类
function Match(data) {
    var t = this;

    t.id = data.id;         // 比赛Id
    t.state = data.st;      // 比赛状态
    t.time = new Date(data.ti);     // 比赛开始时间
    t.mn = data.mn;     // 比赛已进行时间
    t.league = data.lg; // 联赛名
    t.vs = data.vs;     // 比赛的两支球队，0为主队，1为客队
    t.tv = data.tv;     // 转播电视台
    
    // 判断是否已关注
    var allFocused = t.allFocusedMatches();
    if (allFocused != null) {
        for (var i = allFocused.length - 1; i >= 0; i--) {
            if (allFocused[i].id == t.id) {
                t.focused = true;
                break;
            }
        }
    }
    t.focused = t.focused || false;
}

$.extend(Match.prototype, {
    // 关注赛事本地缓存键名
    FOCUSMATCHES : "",
    
    // 获取或设置关注的赛事
    allFocusedMatches : function(val) {
        if (arguments.length) {
            localCache(this.FOCUSMATCHES, val != null ? JSON.stringify(val) : val);
        } else {
            var allFocused = localCache(this.FOCUSMATCHES), i;
            if (allFocused != null && allFocused != "") {
                try {
                    allFocused = JSON.parse(allFocused);
                } catch (e) {
                    allFocused = [ ];
                }
            } else {
                allFocused = [ ];
            }
            
            return allFocused;
        }
    },
    
    // 获取当前比赛的UI HTML元素
    getElement : function() { },

    // 格式化比赛状态
    formatState : function() { },

    // 更新比赛信息
    update : function(pack) {
        var t = this;
        if (pack) {
            // 状态更新
            t.state = pack.st != null ? pack.st : t.state;
            // 开始时间更新
            t.time = pack.ti ? new Date(pack.ti) : t.time;
            // 进行时间更新
            t.mn = pack.mn != null ? pack.mn : t.mn;
            
            var vs = pack.vs;
            if (vs) {
                // 比分更新
                for (var i = vs.length - 1; i >= 0; i--) {
                    if (vs[i].s != null && vs[i].s > t.vs[i].s) {
                        t.vs[i].s = vs[i].s;
                    }
                }
            }
        }

        // 更新视图
        t.updateView && t.updateView();
    },
    
    // 更新视图
    updateView : function() {
        // 替换DOM节点实现更新
        var node = this.getElement();
        if (node) {
            var div = document.createElement("div");
            div.innerHTML = this.toHTML();
            node.parentNode.replaceChild(div.firstChild, node);
            div = null;
        }
    },

    // 通过模板转换为HTML
    toHTML: function() {
        return $.parseTpl(this.tpl, this);
    },

    // 关注比赛
    focus: function() {
        var allFocused = this.allFocusedMatches(), pos = -1, id = this.id;

        for (var i = allFocused.length - 1; i >= 0; i--) {
            if (allFocused[i] == id) {
                pos = i;
                break;
            }
        }

        if (this.focused) {
            if (pos != -1) {
                allFocused.splice(i, 1);
            }
        } else {
            if (-1 == pos) {
                allFocused.push({
                    id : id,
                    time : this.time
                });
            }
        }

        this.focused = !this.focused;
        
        this.allFocusedMatches( allFocused );
    }
});


Match.create = function(data) {
    
};


// 设为全局变量
window.Match = Match;

})(window);